package com.flavors.wan.main.ui.Fragment;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.flavors.wan.main.bean.BaseBean;
import com.flavors.wan.main.bean.CoinBean;
import com.flavors.wan.main.net.BaseApi;
import com.flavors.wan.main.ui.Activity.LoginActivity;
import com.flavors.wan.main.ui.BaseFragment;
import com.flavors.wan.main.vm.WanVM;
import com.lzy.okgo.model.Response;
import com.plat.sdk.R;
import com.plat.sdk.okgo.bean.ResponseBean;
import com.plat.sdk.okgo.callbck.JsonCallback;
import com.plat.sdk.okgo.callbck.XmResource;
import com.plat.sdk.okgo.net.OKUtils;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FourFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FourFragment extends BaseFragment {


    @BindView(R.id.coin_info)
    TextView coininfo;
    @BindView(R.id.login_user)
    Button loginuser;
    @BindView(R.id.coin_btn)
    Button coinBtn;

    private WanVM wanVM;

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_four;
    }

    @Override
    protected void initView() {
        wanVM = ViewModelProviders.of(this).get(WanVM.class);
        wanVM.getCoinBean().observe(this, getCoinObserver());
    }

    @Override
    public void onVisible(boolean isFirstVisible) {
        super.onVisible(isFirstVisible);
        if (isFirstVisible) {
            // TODO: 2020/1/20 第一次由不可见到可见加载数据
            String dateStr = "2020-02-02 00:00:00";
            if (dateStr.trim().length() >= 19) {
                dateStr = dateStr.substring(0, 10);
            }
            Toast.makeText(getContext(), "dateStr:" + dateStr, Toast.LENGTH_SHORT).show();
        }
    }

    public FourFragment() {
    }

    public static FourFragment newInstance() {
        FourFragment fragment = new FourFragment();
        return fragment;
    }


    @OnClick({R.id.login_user, R.id.coin_btn, R.id.exit})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_user:
                startActivity(new Intent(getActivity(), LoginActivity.class));
                break;
            case R.id.exit:
                exitLoginClick();
                break;
            case R.id.coin_btn:
                wanVM.fetchCoin();
                break;
        }


    }


    private void exitLoginClick() {
        OKUtils.getRequest(BaseApi.LOGOUT, this, null, new JsonCallback<ResponseBean<BaseBean>>() {
            @Override
            public void onSuccess(Response<ResponseBean<BaseBean>> response) {
                ResponseBean<BaseBean> body = response.body();
            }

            @Override
            public void onError(Response<ResponseBean<BaseBean>> response) {
                super.onError(response);
            }
        });
    }

    private Observer<XmResource<CoinBean>> getCoinObserver() {
        return coinBeanXmResource -> coinBeanXmResource.handle(new XmResource.OnHandleCallback<CoinBean>() {

            @Override
            public void onLoading() {

            }

            @Override
            public void onSuccess(CoinBean data) {
                coininfo.setText(data.toString());
                Log.d("ydong", "data" + data.toString());
            }

            @Override
            public void onFailure(String message) {

            }

            @Override
            public void onError(int code, String message) {
                Toast.makeText(getContext(), "UI code::" + code + "message::" + message, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCompleted() {

            }
        });
    }
}
